# -*- coding: utf-8 -*-
"""
 @Time    : 2021/2/3 下午5:45
 @Author  : tgy
 
"""


# class Member(object):
#     # 定义描述器实现slots属性的查找
#     def __init__(self, i):
#         self.i = i
#
#     def __get__(self, obj, type=None):
#         return obj._slotvalues[self.i]
#
#     def __set__(self, obj, value):
#         obj._slotvalues[self.i] = value
#
#
# class Type(type):
#     # 使用元类实现slots
#     def __new__(self, name, bases, namespace):
#         slots = namespace.get('_slots_')
#         if slots:
#             for i, slot in enumerate(slots):
#                 namespace[slot] = Member(i)
#             original_init = namespace.get('__init__')
#
#             def __init__(self, *args, **kwargs):
#                 # 创建_slotvalues列表和调用原来的__init__
#                 self._slotvalues = [None] * len(slots)
#                 if original_init:
#                     original_init(self, *args, **kwargs)
#
#             namespace['__init__'] = __init__
#         return type.__new__(self, name, bases, namespace)
#
#
# # Python2与Python3使用元类的区别
# # try:
# #     class Object(object):
# #         __metaclass__ = Type
# # except:
# class Object(metaclass=Type):
#     pass
#
#
# class A(Object):
#     _slots_ = 'x', 'y'
#
#     def __init__(self,x,y):
#         self.x = x
#         self.y = y
#
#
# def test():
#     ...
#     a = A(20,30)
#     # a.x = 10
#     a.opt = 1
#     print(a.opt)


class Person:
    __slots__ = ('name','age')
    def __init__(self, name,age):
        ...
        self.name = name
        self.age = age


if __name__ == '__main__':
    ...
    # test()
    per = Person('zhangsan',21)
    print(Person.name)
    print(dir(per))
    print(getattr(per, 'name'))
